package com.mf.service.impl;

import com.mf.entity.GoodsSpec;
import com.mf.repository.GoodsSpecReponsitory;
import com.mf.service.GoodsSpecService;
import com.mf.util.StringUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@Service("goodsSpecService")
public class GoodsSpecServiceImpl implements GoodsSpecService {

    @Resource
    private GoodsSpecReponsitory goodsSpecReponsitory;

    public List<GoodsSpec> list(GoodsSpec goodsSpec, Integer page, Integer pageSize, Sort.Direction direction, String... properties){
        Pageable pageable = new PageRequest(page - 1, pageSize);

        Page<GoodsSpec> specPage = goodsSpecReponsitory.findAll(new Specification<GoodsSpec>() {
            @Override
            public Predicate toPredicate(Root<GoodsSpec> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = criteriaBuilder.conjunction();
                if(goodsSpec != null){
                    if (goodsSpec.getId() != null) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("id"), "%" + goodsSpec.getId() + "%"));
                    }
                    if (goodsSpec.getGoodsMold() != null && goodsSpec.getGoodsMold().getId() != null) {
                        predicate.getExpressions().add(criteriaBuilder.equal(root.get("goodsMold").get("id"),goodsSpec.getGoodsMold().getId()));
                    }
                    if (StringUtil.isNotEmpty(goodsSpec.getName())) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("name"), "%" + goodsSpec.getName() + "%"));
                    }
                    if (goodsSpec.getSpec_order() != null) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("order"), "%" + goodsSpec.getSpec_order() + "%"));
                    }

                    if (goodsSpec.getSearch_index() != null) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("search_index"), "%" + goodsSpec.getSearch_index() + "%"));
                    }
                }

                return predicate;
            }
        }, pageable);

        return specPage.getContent();

    }

    public Long getCount(GoodsSpec goodsSpec){
        Long count = goodsSpecReponsitory.count(new Specification<GoodsSpec>() {
            @Override
            public Predicate toPredicate(Root<GoodsSpec> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = criteriaBuilder.conjunction();
                if(goodsSpec != null){
                    if (goodsSpec.getId() != null) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("id"), "%" + goodsSpec.getId() + "%"));
                    }
                    if (goodsSpec.getGoodsMold() != null && goodsSpec.getGoodsMold().getId() != null) {
                        predicate.getExpressions().add(criteriaBuilder.equal(root.get("goodsMold").get("id"),goodsSpec.getGoodsMold().getId()));
                    }
                    if (StringUtil.isNotEmpty(goodsSpec.getName())) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("name"), "%" + goodsSpec.getName() + "%"));
                    }
                    if (goodsSpec.getSpec_order() != null) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("order"), "%" + goodsSpec.getSpec_order() + "%"));
                    }

                    if (goodsSpec.getSearch_index() != null) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("search_index"), "%" + goodsSpec.getSearch_index() + "%"));
                    }
                }
                return predicate;
            }
        });
        return count;
    }

    public void delete(Integer id){
        goodsSpecReponsitory.delete(id);
    }

    public void save(GoodsSpec goodsSpec){
        goodsSpecReponsitory.save(goodsSpec);
    }

    @Override
    public GoodsSpec getId(GoodsSpec goodsSpec) {
        return goodsSpecReponsitory.getId(goodsSpec.getGoodsMold().getId(),goodsSpec.getName());
    }
}
